import Vue from 'vue'
import Router from 'vue-router'
import Login from '@/components/Login'
import Home from '@/components/Home'

Vue.use(Router)

/* 给组件配置路由 */
const router = new Router({
  routes: [
    // {path:'/',redirect:'/login'},
    {
      path: '/login',
      component: Login
    },
    {
      path: '/home',
      component: Home
    }
  ]
})

/* 设置路由守卫，禁止非后台用户访问 */
router.beforeEach((to, from, next) => {
  /* 获取token并判断是否存在 */
  var token = window.sessionStorage.getItem('token')
  if (token === null && to.path !== '/login') {
    /* token不存在强制跳转到登录页 */
    return next('/login')
  }
  /* token存在路由继续执行 */
  next()
})

export default router
